package com.oracle.controller;

import com.oracle.Service.impl.LocationService;
import com.oracle.entity.ResultBean;
import com.oracle.pojo.Location;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/address")
public class LocationController {

    @Autowired
    private LocationService locationService;

    @RequestMapping("/delete")
    @ResponseBody
    public ResultBean delete(Integer id){
        Map<String,Object> map =  new HashMap<String,Object>();
        try{
            if (id!=null&&id!=0){
                Integer save = locationService.delete(id);
                if (save==200){
                    return ResultBean.success("删除成功",null);
                }else {
                    return ResultBean.fail("删除失败",null);
                }
            }
            return ResultBean.fail("没有该用户",null);
        }catch (Exception e){
            return ResultBean.fail("没有该用户",null);
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public ResultBean update(@Validated Location location, Errors errors){
        System.out.println(location);
        Map<String,Object> map =  new HashMap<String,Object>();
        try{
            if (location!=null){
                if (errors!=null&&errors.getFieldErrors()!=null&&errors.getFieldErrors().size()>0){
                    for (FieldError fieldError:errors.getFieldErrors()){
                        map.put(fieldError.getField()+"Error",fieldError.getDefaultMessage());
                    }
                    return ResultBean.getResultBean(300,"地址信息有空值",map);
                }else{
                    Integer save = locationService.update(location);
                    if (save==200){
                        return ResultBean.success("修改地址成功",location);
                    }else {
                        return ResultBean.fail("修改失败",null);
                    }
                }
            }
            return ResultBean.fail("没有该用户",null);
        }catch (Exception e){
            return ResultBean.fail("没有该用户",null);
        }
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultBean save(@Validated Location location, Errors errors){
        System.out.println(location);
        Map<String,Object> map =  new HashMap<String,Object>();
        try{
            if (location!=null){
                if (errors!=null&&errors.getFieldErrors()!=null&&errors.getFieldErrors().size()>0){
                    for (FieldError fieldError:errors.getFieldErrors()){
                        map.put(fieldError.getField()+"Error",fieldError.getDefaultMessage());
                    }
                    return ResultBean.getResultBean(300,"地址信息有空值",map);
                }else{
                    Integer save = locationService.save(location);
                    if (save==200){
                        return ResultBean.success("新增地址成功",location);
                    }else {
                        return ResultBean.fail("没有该用户",null);
                    }
                }
            }
            return ResultBean.fail("没有该用户",null);
        }catch (Exception e){
            return ResultBean.fail("没有该用户",null);
        }
    }


    @RequestMapping("/findSome")
    @ResponseBody
    public ResultBean findSome(Integer uid){
        try{
            if (uid!=null&&uid!=0){
                List<Location> some = locationService.findSome(uid);
                if (some==null){
                    return ResultBean.getResultBean(300,"该用户没有收获地址，请添加收货地址！",null);
                }
                return ResultBean.success("该用户所有的收货地址",some);
            }else {
                return ResultBean.fail("没有该用户",null);
            }
        }catch (Exception e){
            return ResultBean.fail("没有该用户",null);
        }
    }

    @RequestMapping("/findOne")
    @ResponseBody
    public ResultBean findOne(Integer uid){
        try{
            if (uid!=null&&uid!=0){
                Location location = locationService.findOne(uid, 1);
                if (location==null){
                    return ResultBean.getResultBean(300,"没有默认地址，请设置默认收货地址！",null);
                }
                return ResultBean.success("该用户所有的收货地址",location);
            }else {
                return ResultBean.fail("没有该用户",null);
            }
        }catch (Exception e){
            return ResultBean.fail("没有该用户",null);
        }
    }
}
